package com.ambow.shop.action.impl;

import java.util.Locale.Category;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ambow.shop.action.Action;
import com.ambow.shop.domain.Product;
import com.ambow.shop.domain.ProductCategory;
import com.ambow.shop.service.ProductCategoryService;
import com.ambow.shop.service.ProductService;
import com.ambow.shop.service.impl.ProductCategoryServiceImpl;
import com.ambow.shop.service.impl.ProductServiceImpl;

public class GetProductAction implements Action {
	
	private ProductService productService = new ProductServiceImpl();
	private ProductCategoryService categoryService = new ProductCategoryServiceImpl();
	
	@Override
	public String execute(HttpServletRequest request, HttpServletResponse response) {
		// 如果此时要根据参数不同，显示不同的餐食的内容？
		// 1、在发送请求(jsp)时，使用标识来区分；../product/get?id=${product.id }
		// 2、在servlet中接受参数；
		String id = request.getParameter("id");
		// 万一格式不正确，这里还会有异常；（校验）；
		// 3、转换
		Integer productId = Integer.parseInt(id);
		// 4、调用；
		Product product = productService.getProduct(productId);	
		// 从上下文中取得类别；
		ProductCategory category = categoryService.getProductCategory(product.getCategory().getId());
		product.setCategory(category);
		
		request.setAttribute("product", product);		
		// 返回视图的URL；
		return "../view/productDetail.jsp";
	}

}
